﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using WcfGeebaServer.ContentManage;
using System.Collections.Generic;
using WcfGeebaServer.Users;
using WcfGeebaServer.Utils;
using WcfGeebaServer.Policies;
using WcfGeebaServer.Logging;

namespace WcfGeebaServer
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class SLService : ServiceCore, ISLService
    {
        //static AsyncCallback _UpdateCompleted = new AsyncCallback(UpdateCompleted);
        //static AsyncCallback _EditCompleted = new AsyncCallback(EditCompleted);

        SLService() : base() { }

        public void slSubscribe(string username)
        {
            try
            {
                ISListener listener = OperationContext.Current.GetCallbackChannel<ISListener>();
                String ID = OperationContext.Current.SessionId;

                SubscribersType group = new SubscribersType { Username = username, SessionId = ID, SListener = listener };
                subscribers.RemoveAll((x) => x.SListener == listener && x.SessionId == ID);

                if (forum.Policy.CanSubscribe(username))
                    subscribers.Add(group);

                //return true;
            }
            catch (Exception e)
            {
                GLogger.error("subscribe", e);
                //return false;
            }
        }
        public void slUnsubscribe()
        {
            try
            {
                ISListener listener = OperationContext.Current.GetCallbackChannel<ISListener>();
                String ID = OperationContext.Current.SessionId;

                subscribers.RemoveAll((x) => x.SessionId == ID);
                //return true;
            }
            catch (Exception e)
            {
                GLogger.error("unsubscribe", e);
                //return false;
            }
        }

        //protected override void notifyListeners(GThread message)
        //{
        //    try
        //    {
        //        foreach (SubscribersType listenterEntry in subscribers)
        //        {
        //            if (forum.Policy.CanSubscribe(listenterEntry.Username))
        //            {
        //                if (((ICommunicationObject)listenterEntry.SListener).State == CommunicationState.Opened)
        //                {
        //                    Member member = forum.GetMember(listenterEntry.Username);
        //                    bool isNotification = forum.Policy.IsNotification(member, message);
        //                    try
        //                    {
        //                        listenterEntry.SListener.BeginUpdate(message, DateTime.Now, isNotification, _UpdateCompleted, listenterEntry.SListener);
        //                    }
        //                    catch (TimeoutException texp)
        //                    {
        //                        GLogger.error("notifyListeners timeout", texp);
        //                    }
        //                    catch (CommunicationException cexp)
        //                    {
        //                        GLogger.error("notifyListeners communication", cexp);   
        //                    }
        //                }
        //                else
        //                {
        //                    subscribers.Remove(listenterEntry);
        //                }
        //            }
        //            else
        //            {
        //                subscribers.RemoveAll((x) => x.Username == listenterEntry.Username);
        //            }

        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        GLogger.error("notifyListeners", e);
        //    }
        //}
        //protected override void notifyListenersMessageEdited(string subforumTopic, GThread message)
        //{
        //    try
        //    {
        //        foreach (SubscribersType listenterEntry in subscribers)
        //        {
        //            if (((ICommunicationObject)listenterEntry.SListener).State == CommunicationState.Opened)
        //            {
        //                try
        //                {
        //                    listenterEntry.SListener.BeginEditMessage(subforumTopic, message, DateTime.Now, _EditCompleted, listenterEntry.SListener);
        //                }
        //                catch (TimeoutException texp)
        //                {
        //                    GLogger.error("notifyListeners timeout", texp);
        //                }
        //                catch (CommunicationException cexp)
        //                {
        //                    GLogger.error("notifyListeners communication", cexp);
        //                }
        //            }
        //            else
        //            {
        //                subscribers.Remove(listenterEntry);
        //            }
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        GLogger.error("notifyListenersMessageEdited", e);
        //    }
        //}
        
        //private static void EditCompleted(IAsyncResult result)
        //{
        //    try
        //    {
        //        ((ISListener)(result.AsyncState)).EndEditMessage(result);
        //    }
        //    catch (CommunicationException)
        //    {
        //        // empty
        //    }
        //    catch (TimeoutException)
        //    {
        //        // empty
        //    }
        //}
        //private static void UpdateCompleted(IAsyncResult result)
        //{
        //    try
        //    {
        //        ((ISListener)(result.AsyncState)).EndUpdate(result);
        //    }
        //    catch (CommunicationException)
        //    {
        //        // empty
        //    }
        //    catch (TimeoutException)
        //    {
        //        // empty
        //    }
        //}
    }
}
